#include "fig_moving_particles.h"
#include "num/rand.h"
#include "dd/dd_man.h"
#include "graphics/graphics_util.h"

FIGMovingParticles::FIGMovingParticles()
{
	
	m_Sprite.Create("spot");
	for(int i = 0;i < MAX_PARTICLE_COUNT; i++)
	{
		
		FIGMovingParticles::FIGMovingParticle* Particle = new FIGMovingParticles::FIGMovingParticle();
		

		float R =  num::RAND32::Instance().RandFloat( 0, 5.0f);
		float G	=  num::RAND32::Instance().RandFloat( 0, 5.0f);
		float B =  num::RAND32::Instance().RandFloat( 0, 5.0f);

		
		Particle->m_Speed = num::RAND32::Instance().RandFloat(0.5f,2.0f);
		Particle->m_BaseColor	=   ColorfRGBA(R,G,B, Particle->m_Speed + 0.4f);
		
		float X = num::RAND32::Instance().RandFloat( 0, 1.0f);
		float Y = num::RAND32::Instance().RandFloat( 0, 1.0f);		
		Particle->m_Pos =   math::Vec2( X , Y);
		
		m_MovingParticles.push_back(Particle);

	}
}

FIGMovingParticles::~FIGMovingParticles()
{
	for(int i = 0; i < (int)m_MovingParticles.size(); i++)
	{
		UTIL_SAFE_DELETE(m_MovingParticles[i]);
	}
	m_MovingParticles.resize(0);
}


FIGMovingParticles&		FIGMovingParticles::Instance()
{
	static FIGMovingParticles	Inst;
	return Inst;
}


void		FIGMovingParticles::Render(const math::Vec2& Offset, float Scale)
{
	
	 for(int i = 0;i < MAX_PARTICLE_COUNT; i++)
	 {
		 FIGMovingParticles::FIGMovingParticle* Particle =	m_MovingParticles[i];
		 
		 math::Vec2 Offset1 = Offset * ( Particle->m_Speed * 0.5f);

		 m_Sprite.m_Pos = Particle->m_Pos + Offset1;
		 m_Sprite.m_Color = Particle->m_BaseColor;
		 m_Sprite.m_Size = m_Sprite.m_SizeOriginal * Scale;
		 m_Sprite.RenderRotated();
		 
		 
		 //AddCircle2D(Particle->m_Pos,Pixel,Particle->m_BaseColor,false);
		  
	 }
}

void		FIGMovingParticles::Tick(float dt)
{
	m_Sprite.Tick(dt);
	for(int i = 0;i < MAX_PARTICLE_COUNT; i++)
	{
		
		FIGMovingParticles::FIGMovingParticle* Particle =	m_MovingParticles[i];
		Particle->m_Pos.y += ( 0.15f * dt * Particle->m_Speed );

		if( Particle->m_Pos.y > 1.0f)
		{
		float R =  num::RAND32::Instance().RandFloat( 0, 1.0f);
		float G	=  num::RAND32::Instance().RandFloat( 0, 1.0f);
		float B =  num::RAND32::Instance().RandFloat( 0, 1.0f);

		
		Particle->m_Speed = num::RAND32::Instance().RandFloat(0.5f,2.0f);
		Particle->m_BaseColor	=   ColorfRGBA(R,G,B, Particle->m_Speed + 0.5f);
		
		float X = num::RAND32::Instance().RandFloat( 0, 1.0f);
		float Y = -0.1f;
		Particle->m_Pos =   math::Vec2( X , Y);
		}
		
	}
}
	
void		FIGMovingParticles::Clear()
{

}
